import {onUpdated, onMounted, ref} from 'vue'

const formatTime = (value, type = 'seconds') => {
    let date = new Date(value)
    let Y = date.getFullYear()
    let M = date.getMonth() + 1
    M = M < 10 ? '0' + M : M
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let m = date.getMinutes()
    m = m < 10 ? '0' + m : m
    let s = date.getSeconds()
    s = s < 10 ? '0' + s : s
    if (type === 'seconds') {
        // 精确到秒
        return Y + '-' + M + '-' + d + ' ' + h + ':' + m + ':' + s
    } else {
        // 精确到小时
        return Y + '-' + M + '-' + d + ' ' + h
    }
}
const calcTableHeight = (height) => {

    const tableRef = ref(null)

    let tableHeight = ref(300)

    const table = () => {
        if (tableRef.value) {
            setTimeout(() => {
                if (height) {
                    tableHeight.value = height
                } else {
                    if (tableRef.value) {
                        // 计算表格的高度：窗口高度减去表格元素距离窗口顶部的距离再减去(分页高度)
                        tableHeight.value = window.innerHeight - tableRef.value.$el.offsetTop - 66
                    }
                }
            }, 10)
        }
    }
    // 假数据不会触发更新
    onUpdated(() => {
        table()
        window.addEventListener('resize', table)
    })
    onMounted(() => {
        table()
        window.addEventListener('resize', table)
    })
    return {
        tableRef,
        tableHeight
    }
}

export {
    formatTime,
    calcTableHeight
}

